#!/bin/bash

set -e

rm -rf bin
rm -f main.cc
rm -f main2.cc
cp main_orig.cc main.cc

cleanup_exit() {
  echo "Test failed"
  exit ${1}
}

echo "Build should succeed"
../../build.py build :main || cleanup_exit 1

../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph1 - || cleanup_exit 1

touch dep/BUILD

echo "Build should succeed"
../../build.py build :t2 || cleanup_exit 1
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph2 - || cleanup_exit 1

echo "Build should succeed"
../../build.py build :main || cleanup_exit 1
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph3 - || cleanup_exit 1

echo "Build should fail"
mv main.cc main2.cc
../../build.py build :main && cleanup_exit 1

echo "Build should succeed"
mv main2.cc main.cc
../../build.py build :main || cleanup_exit 1

rm -rf bin
rm -f main.cc
rm -f main2.cc

echo 'Test passed'
